﻿using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Logging;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Transactions;
using Traceability_Management.Domain;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Dto;

namespace Traceability_Management.Read.Api.Controllers.ExIM
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExportAllController : ControllerBase
    {

        IBaseReponsitory<Rabits> Rabits;
        IBaseReponsitory<Shop_Table> Shop_Table;
        IBaseReponsitory<Exports> Exports;

        public ExportAllController(IBaseReponsitory<Rabits> Rabits, IBaseReponsitory<Shop_Table> Shop_Table, IBaseReponsitory<Exports> Exports)
        {
            this.Rabits = Rabits;
            this.Shop_Table = Shop_Table;
            this.Exports = Exports;
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="Size"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Export(int NumCout)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    Exports exports = new Exports();
                    exports.Export_Id = 0;
                    exports.Export_Count = NumCout;
                    await Exports.AddAsync(exports);
                    var list = from a in Rabits.GetQuery()
                               join b in Shop_Table.GetQuery()
                              on a.SId equals b.Shop_Id
                               select new RabitDto
                               {
                                   Rabit_Id = a.Rabit_Id,
                                   Shop_Id = a.SId,
                                   Shop_Name = b.Shop_Name,
                                   Rabit_Batch = a.Rabit_Batch,
                                   Rabit_Shelflife = a.Rabit_Shelflife,
                                   Rabit_Shencan = a.Rabit_Shencan,
                                   Rabit_Company = a.Rabit_Company,
                                   Rabit_Infomation = a.Rabit_Infomation,
                                   Rabit_TitleImg = a.Rabit_TitleImg,
                                   Rabit_IsDel = a.Rabit_IsDel,
                                   Creater = a.Creater,
                                   CreateTime = a.CreateTime,
                                   Updater = a.Updater,
                                   UpdateTime = a.UpdateTime
                               };


                    var slist = list.OrderByDescending(x => x.Rabit_Id).Where(c => !c.Rabit_IsDel).ToList();
                    IWorkbook workbook = new XSSFWorkbook();
                    ISheet sheet = workbook.CreateSheet("工作簿");
                    IRow col = sheet.CreateRow(0);
                    col.CreateCell(0).SetCellValue("序号");
                    col.CreateCell(1).SetCellValue("商品名称");
                    col.CreateCell(2).SetCellValue("生产批次");
                    col.CreateCell(3).SetCellValue("保质期(天)");
                    col.CreateCell(4).SetCellValue("生产许可证号");
                    col.CreateCell(5).SetCellValue("信息完善度");
                    col.CreateCell(6).SetCellValue("创建人");
                    col.CreateCell(7).SetCellValue("创建时间");
                    col.CreateCell(8).SetCellValue("修改人");
                    col.CreateCell(9).SetCellValue("修改时间");
                    for (int i = 0; i < slist.Count(); i++)
                    {
                        if (NumCout > 0 && i >= NumCout)
                        {

                            break;
                        }
                        IRow scol = sheet.CreateRow(i + 1);
                        scol.CreateCell(0).SetCellValue(slist[i].Rabit_Id);
                        scol.CreateCell(1).SetCellValue(slist[i].Shop_Name);
                        scol.CreateCell(2).SetCellValue(slist[i].Rabit_Batch);
                        scol.CreateCell(3).SetCellValue(slist[i].Rabit_Shelflife);
                        scol.CreateCell(4).SetCellValue(slist[i].Rabit_Shencan);
                        scol.CreateCell(5).SetCellValue(slist[i].Rabit_Infomation);
                        scol.CreateCell(6).SetCellValue(slist[i].Creater);
                        scol.CreateCell(7).SetCellValue(slist[i].CreateTime.ToString().Substring(0, 10));
                        scol.CreateCell(8).SetCellValue(slist[i].Updater);
                        if (slist[i].UpdateTime != null)
                        { scol.CreateCell(9).SetCellValue(slist[i].UpdateTime.ToString().Substring(0, 10)); }
                        else { scol.CreateCell(9).SetCellValue(""); }

                    }
                    byte[] sz;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workbook.Write(ms);
                        sz = ms.ToArray();
                    }
                    var ExType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];

                    scope.Complete();

                    return File(sz, ExType, "工作簿.xlsx");
                }
                catch (Exception)
                {
                    throw;
                }
            }




        }
    }
}
